package com.xb.service.impl;

import com.xb.bean.TokenBean;
import com.xb.bean.UserBean;
import com.xb.exception.BizException;
import com.xb.mapper.UserMapper;
import com.xb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public TokenBean login(UserBean userBean) {
        UserBean user = userMapper.selectByUP(userBean);
        TokenBean tokenBean = new TokenBean();
        if (user == null) {
            throw new BizException("账号或密码错误");
        }
        tokenBean.setToken(user.getId());
        return tokenBean;
    }

    @Override
    public UserBean getUserInfo(String authorization) {
        String[] authStr = authorization.split(" ");
        int userId = Integer.parseInt(authStr[1]);
        UserBean userBean = userMapper.selectUserById(userId);
        if(userBean == null){
            throw new BizException("暂无用户信息");
        }
        return userBean;
    }
}
